﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace System.Drawing
{
    public static class ImageExtension
    {
        public static Bitmap AddBorder(this Bitmap image)
        {
            Bitmap bitmap = new Bitmap(image.Width + 15, image.Height + 15);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            for (int i = 0; i <= bitmap.Height; i++)
            {
                graphics.DrawLine(new Pen(Color.FromArgb(0xff, 0xff, 0xff, 0xff)), 0, i, bitmap.Width, i);
            }
            int num2 = 5;
            for (int j = 0; j <= num2; j++)
            {
                graphics.DrawLine(new Pen(Color.FromArgb(0xff, 0x37 + (j * 40), 0x37 + (j * 40), 30 + (j * 40))), 0, (bitmap.Height - num2) + j, bitmap.Width, (bitmap.Height - num2) + j);
                graphics.DrawLine(new Pen(Color.FromArgb(0xff, 0x37 + (j * 40), 0x37 + (j * 40), 30 + (j * 40))), (bitmap.Width - num2) + j, 0, (bitmap.Width - num2) + j, bitmap.Height);
                graphics.DrawLine(new Pen(Color.FromArgb(0xff, 0x37 + (j * 40), 0x37 + (j * 40), 30 + (j * 40))), num2 - j, bitmap.Height - num2, num2 - j, bitmap.Height);
                graphics.DrawLine(new Pen(Color.FromArgb(0xff, 0x37 + (j * 40), 0x37 + (j * 40), 30 + (j * 40))), bitmap.Width - num2, num2 - j, bitmap.Width, num2 - j);
            }
            graphics.DrawRectangle(new Pen(Color.FromArgb(0, 0, 0), 1f), 0, 0, bitmap.Width - 5, bitmap.Height - 5);
            graphics.DrawImage(new Bitmap(image, image.Width, image.Height), 6, 6);
            return new Bitmap(bitmap, image.Width, image.Height);
        }
        public static Bitmap CompressImage(this Bitmap image, int quality)
        {
            if ((quality < 0) || (quality > 100))
            {
                throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");
            }
            EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
            ImageCodecInfo encoderInfo = GetEncoderInfo("image/jpeg");
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = parameter;
            MemoryStream stream = new MemoryStream();
            image.Save(stream, encoderInfo, encoderParams);
            return new Bitmap(stream);
        }
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
            for (int i = 0; i <= (imageEncoders.Length - 1); i++)
            {
                if (imageEncoders[i].MimeType == mimeType)
                {
                    return imageEncoders[i];
                }
            }
            return null;
        }
        public static Bitmap GetProportionalResizedImage(this Bitmap image, int maxWidth, int maxHeight)
        {
            return image.GetProportionalResizedImage(maxWidth, maxHeight, false, false);
        }
        public static Bitmap GetProportionalResizedImage(this Bitmap image, int maxWidth, int maxHeight, bool allowEnlargement, bool addBorder)
        {
            float num;
            if ((!allowEnlargement && (image.Height < maxHeight)) && (image.Width < maxWidth))
            {
                return image;
            }
            if (image.Height > image.Width)
            {
                num = ((float)image.Height) / ((float)maxHeight);
                maxWidth = (short)Math.Round((double)(((float)image.Width) / num));
            }
            else
            {
                num = ((float)image.Width) / ((float)maxWidth);
                maxHeight = (short)Math.Round((double)(((float)image.Height) / num));
            }
            if (addBorder)
            {
                return new Bitmap(image, maxWidth, maxHeight).AddBorder();
            }
            return new Bitmap(image, maxWidth, maxHeight);
        }
        public static Bitmap GetResizedImage(this Bitmap image, int width, int height)
        {
            return new Bitmap(image, width, height);
        }
        public static void SaveToJpgStream(this Bitmap image, Stream stream)
        {
            image.Save(stream, ImageFormat.Jpeg);
        }

        public static Bitmap Contour(int width, int height, List<Point> points)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(b);
            g.Clear(Color.White);
            g.DrawPolygon(Pens.Black, points.ToArray());
            g.FillPolygon(Brushes.Green, points.ToArray());

            //b = b.GetProportionalResizedImage(width, height, true, false);

            return b;
        }
    }
}
